﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;

namespace CNKI.TPI.Web.Base
{
    public class EncryptHelper
    {
        private static byte[] key = Convert.FromBase64String("S7DLSRcOALUJJ11Repa3ouq7MVMaxePg");
        private static byte[] iv = Convert.FromBase64String("89vzUpX2wss=");
        
        public static string Encrypt(string content)
        {
            string result = "";
            //try
            //{
            //    CspParameters param = new CspParameters();
            //    param.KeyContainerName = key;//密匙容器的名称，保持加密解密一致才能解密成功
            //    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            //    {
            //        byte[] plaindata = Encoding.Default.GetBytes(content);//将要加密的字符串转换为字节数组
            //        byte[] encryptdata = rsa.Encrypt(plaindata, false);//将加密后的字节数据转换为新的加密字节数组
            //        result = Convert.ToBase64String(encryptdata);//将加密后的字节数组转换为字符串
            //    }
            //}
            //catch
            //{ 
            //}

            //创建一个内存流
            MemoryStream memoryStream = new MemoryStream();
            //使用传递的私钥和IV创建加密流
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                new TripleDESCryptoServiceProvider().CreateEncryptor(key, iv),
                CryptoStreamMode.Write);
            //将传递的字符串转换为字节数组
            byte[] toEncrypt = Encoding.Default.GetBytes(content);
            try
            {
                //将字节数组写入加密流,并清除缓冲区
                cryptoStream.Write(toEncrypt, 0, toEncrypt.Length);
                cryptoStream.FlushFinalBlock();
                //得到加密后的字节数组
                byte[] encryptedBytes = memoryStream.ToArray();
                result = Convert.ToBase64String(encryptedBytes);
            }
            catch (CryptographicException err)
            {
                throw new Exception("加密出错：" + err.Message);
            }
            finally
            {
                cryptoStream.Close();
                memoryStream.Close();
            }
            return HttpUtility.UrlEncode(result);
        }

        public static string Decrypt(string content)
        {
            string result = "";
            //try
            //{
            //    CspParameters param = new CspParameters();
            //    param.KeyContainerName = key;
            //    using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            //    {
            //        byte[] encryptdata = Convert.FromBase64String(content);
            //        byte[] decryptdata = rsa.Decrypt(encryptdata, false);
            //        result = Encoding.Default.GetString(decryptdata);
            //    }
            //}
            //catch
            //{ 
            //}
            byte[] dataBytes = Convert.FromBase64String(content);
            //根据加密后的字节数组创建一个内存流
            MemoryStream memoryStream = new MemoryStream(dataBytes);
            //使用传递的私钥、IV和内存流创建解密流
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                new TripleDESCryptoServiceProvider().CreateDecryptor(key, iv),
                CryptoStreamMode.Read);
            //创建一个字节数组保存解密后的数据
            byte[] decryptBytes = new byte[dataBytes.Length];
            try
            {
                //从解密流中将解密后的数据读到字节数组中
                cryptoStream.Read(decryptBytes, 0, decryptBytes.Length);
                //得到解密后的字符串
                result = Encoding.Default.GetString(decryptBytes);
            }
            catch (CryptographicException err)
            {
                throw new Exception("解密出错：" + err.Message);
            }
            finally
            {
                cryptoStream.Close();
                memoryStream.Close();
            }

            return result.TrimEnd('\0');
        }
    }
}
